import request from '@/utils/request';

const formatData = params => {
  const formdata = new FormData();
  formdata.append('username', params.username);
  formdata.append('password', params.password);
  formdata.append('code', params.code);
  formdata.append('captchaId', params.captchaId);
  return formdata;
};

export async function fakeAccountLogin(params) {
  return request('/login', {
    method: 'POST',
    notoken: true,
    data: formatData(params),
  });
}

export async function initCaptcha() {
  return request('/init');
}

export async function drawCaptcha(id) {
  return request(`/draw/${id}`, {
    responseType: 'blob',
  });
}

export async function getFakeCaptcha(mobile) {
  return request(`/api/login/captcha?mobile=${mobile}`);
}

export async function getMenuData(params) {
  return request('/menu', { params });
}

export const formatList = params => {
  const formdata = new FormData();
  for (const key in params) {
    formdata.append(key, params[key]);
  }
  return formdata;
};

// 扫码登录
export async function qrCodeLogin(params) {
  return request('/qrLogin', {
    method: 'POST',
    notoken: true,
    data: formatList(params),
  });
}
